package com.cjh.wechat.core.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.cjh.wechat.commom.pojo.Group;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.type.MessType;
import com.cjh.wechat.core.service.GroupService;
import com.cjh.wechat.core.service.UserService;

@Controller
@RequestMapping("/direct")
public class DirectController
{
    @Resource
    private UserService        userService;
    @Resource
    private GroupService       groupService;
    public final static String UPLOAD_ADDR      = "http://localhost/wechat-upload";
    public final static String WEBSOCKET_ADDR   = "ws://localhost/wechat-websocket/websocket";
    public final static String ADD_SESSION_ADDR = "http://localhost/wechat-websocket/account/addSession";
    
    @RequestMapping(value = "/directLogin", method = RequestMethod.GET)
    public ModelAndView directLogin()
    {
        ModelAndView mv = new ModelAndView();
        mv.addObject("addSessionAddr", ADD_SESSION_ADDR);
        mv.setViewName("login.html");
        return mv;
    }
    
    @RequestMapping(value = "/directMain", method = RequestMethod.GET)
    public ModelAndView directMain()
    {
        ModelAndView mv = new ModelAndView();
        mv.addObject("uploadAddr", UPLOAD_ADDR);
        mv.addObject("websocketAddr", WEBSOCKET_ADDR);
        mv.setViewName("main.html");
        return mv;
    }
    
    @RequestMapping(value = "/directMsgbox", method = RequestMethod.GET)
    public ModelAndView directMsgbox(String id)
    {
        User user = userService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("userInfo", user);
        mv.setViewName("msgbox.html");
        return mv;
    }
    
    @RequestMapping(value = "/directFind", method = RequestMethod.GET)
    public ModelAndView directFind(String id)
    {
        User user = userService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("userInfo", user);
        mv.setViewName("find.html");
        return mv;
    }
    
    @RequestMapping(value = "/directUserInfo", method = RequestMethod.GET)
    public ModelAndView directUserInfo(String id)
    {
        User user = userService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("userInfo", user);
        mv.addObject("uploadAddr", UPLOAD_ADDR);
        mv.setViewName("user_info.html");
        return mv;
    }
    
    @RequestMapping(value = "/directGroupMsgbox", method = RequestMethod.GET)
    public ModelAndView directGroupMsgbox(String gid)
    {
        Group group = groupService.getByGid(gid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("group", group);
        mv.setViewName("group_msgbox.html");
        return mv;
    }
    
    @RequestMapping(value = "/directChatRecord", method = RequestMethod.GET)
    public ModelAndView directChatRecord(String id, String type, HttpSession session)
    {
        User user = (User) session.getAttribute("USERINFO");
        String myid = user.getId();
        if (type.equals(MessType.FRIEND.value))
        {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("chat_record.html");
            mv.addObject("type", type);
            mv.addObject("myId", myid);
            mv.addObject("friendId", id);
            mv.addObject("groupId", "");
            return mv;
        }
        else
        {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("chat_record.html");
            mv.addObject("type", type);
            mv.addObject("myId", "");
            mv.addObject("friendId", "");
            mv.addObject("groupId", id);
            return mv;
        }
    }
}
